package com.business.form.controller.gen;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.business.form.model.auto.FormInfo;
import com.business.form.service.FormFieldInfoService;
import com.business.form.service.FormInfoService;
import com.business.system.common.base.BaseController;
import com.business.system.common.domain.AjaxResult;
import com.business.system.model.custom.TableSplitResult;
import com.business.system.model.custom.Tablepar;
import com.business.system.model.custom.TitleVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("FormInfoController")
@Api(value = "表单详情")
public class FormInfoController extends BaseController {
	
	private String prefix = "gen/formInfo";
	@Autowired
	private FormInfoService formInfoService;
	@Autowired
	private FormFieldInfoService formFieldInfoService;


	@ApiOperation(value="表单编辑页面",notes="表单编辑页面")
	@GetMapping("/formEditIndex")
	public String formEditIndex(HttpServletRequest req,ModelMap map) {
		String str="前台";
		setTitle(map, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
		String formId=req.getParameter("form_id");
		map.put("form_id",formId);
		return prefix + "/formEditIndex";
	}

	@GetMapping("view")
	/*@RequiresPermissions("gen:formInfo:view")*/
    public String view(ModelMap model)
    {	
		String str="表单详情";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	//@Log(title = "表单详情集合查询", action = "111")
	@RequestMapping("list")
	//@RequiresPermissions("gen:formInfo:list")
	@ResponseBody
	public Object list(Tablepar tablepar, String searchTxt){
		PageInfo<FormInfo> page=formInfoService.list(tablepar,searchTxt) ; 
		TableSplitResult<FormInfo> result=new TableSplitResult<FormInfo>(page.getPageNum(), page.getTotal(), page.getList());
		return  result;
	}
	
	/**
     * 新增
     */

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }
	
	//@Log(title = "表单详情新增", action = "111")
	@PostMapping("add")
	//@RequiresPermissions("gen:formInfo:add")
	@ResponseBody
	public AjaxResult add(FormInfo formInfo){
		int b=formInfoService.insertSelective(formInfo);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	//@Log(title = "表单详情删除", action = "111")
	@PostMapping("remove")
	//@RequiresPermissions("gen:formInfo:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=formInfoService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查
	 * @param
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(FormInfo formInfo){
		int b=formInfoService.checkNameUnique(formInfo);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("FormInfo", formInfoService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "表单详情修改", action = "111")
    //@RequiresPermissions("gen:formInfo:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormInfo record)
    {
        return toAjax(formInfoService.updateByPrimaryKeySelective(record));
    }


    /*
    根据formid获取表单定义的字段信息 用户渲染表单或者编辑表单
     */
	@RequestMapping("/getFormJson")
	@ResponseBody
	public AjaxResult getFormJson(HttpServletRequest req){
		String formId=req.getParameter("form_id");
		List<HashMap<String,String>> list=formFieldInfoService.getFormFieldJson2(formId);
		return AjaxResult.success().put("formJson",list);
	}


	/**
	 * 获取表单编辑器的表单字段信息 为json格式 根据表单字段json 保存表单信息
	 * @param req
	 * @return
	 */
	@RequestMapping("/editForm")
	@ResponseBody
	public AjaxResult editForm(HttpServletRequest req){
		String form_id=req.getParameter("form_id");
		String jsons=req.getParameter("json");
		JSONArray arr=JSONArray.parseArray(jsons);
		for(int i=0;i<arr.size();i++){
			JSONObject obj=arr.getJSONObject(i);
			System.out.println(obj.getString("label"));
		}
		FormInfo form=formFieldInfoService.addFormField(form_id,arr);
		return AjaxResult.success().put("form",form);
	}

	/**
	 * 根据formId查询表单信息
	 * @param formId
	 * @return
	 */
	@GetMapping("selectOne/{formId}")
	@ResponseBody
	public AjaxResult selectOne(@PathVariable("formId") String formId){
		FormInfo formInfo = formInfoService.selectByPrimaryKey(formId);
		return AjaxResult.success().put("formInfo",formInfo);
	}

	
}
